using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ModelBinder.Host.Infrastructures.Middlewares;
using ModelBinder.Host.Infrastructures.ModelBinders;
using ModelBinder.Web.Infrastructures.ApplicationModelProviders;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews(opt =>
{
    opt.ModelBinderProviders.Insert(0, new CustomBodyWrapModelBinderProvider(opt));
});
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IApplicationModelProvider, CustomApiBehaviorApplicationModelProvider>());
var serviceDescriptor = builder.Services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IModelBinderFactory));
builder.Services.Remove(serviceDescriptor);
builder.Services.TryAddSingleton<IModelBinderFactory, CustomModelBinderFactory>();
var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseMiddleware<HttpBodyMiddleware>();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();

public partial class Program { }